package xyz.eden.gateway.springcloud.admin.protocol.iplist;

import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Slf4j
@Data
@Configuration
@RefreshScope
@NacosConfigurationProperties(prefix = "gateway.ip.service.instance", dataId = "gateway")
@ConfigurationProperties(prefix = "gateway.ip.service.instance")
public class IpClientServiceInstanceSourceConfig {

    private List<IpClientServiceInstanceProperties> configList;

    @Value(value = "${gateway.service.instance.ip:}")
    private String mapStr;

    public List<IpClientServiceInstance> getServiceInstanceList(String serviceId) {
        return Optional.ofNullable(configList).orElse(Collections.emptyList())
                .stream().filter(config -> {
                    return Objects.equals(config.getServiceId(), serviceId);
                }).findFirst().map(IpClientServiceInstanceProperties::builderServiceInstance).orElse(Collections.emptyList());
    }
}
